package com.spottydog.virtues;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class QuoteActivity extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.paper);
		setContentView(R.layout.quote);
		
		InputStream is = getResources().openRawResource(R.raw.quotes);
		Writer w = new StringWriter();
		char[] buffer = new char[1024];
		try {
			Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			int n;
			while ((n = reader.read(buffer)) != -1) {
				w.write(buffer, 0, n);
			}
		} catch (IOException e) {
		} finally {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
		
		String[] quotes = w.toString().split("\n");
		
		TextView quoteText = (TextView) findViewById(R.id.quote_text);		
		Typeface face = Typeface.createFromAsset(getAssets(), "fonts/BenjaminFranklin.ttf");
		quoteText.setTypeface(face);
		
		// New quote each day only
		Date today = new Date();
		today.setHours(0);
		today.setMinutes(0);
		today.setSeconds(0);
		Random random = new Random((today.getTime()/1000));
		
		TextView qotd = (TextView) findViewById(R.id.qotd);
		qotd.setTypeface(face);
		qotd.setText("Quote for " + SimpleDateFormat.getDateInstance(DateFormat.LONG).format(today));
		
		TextView another = (TextView) findViewById(R.id.go_back);
		another.setTypeface(face);
		another.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				QuoteActivity.this.finish();			
			}
		});
		
		quoteText.setText("\"" + quotes[random.nextInt(quotes.length)] + "\"");
	}
}
